%% Nonlinear Simulations
% by Jaromir Benes
%
% In this tutorial, we show how to simulate models in an exact nonlinear
% mode (equivalent to perfect-foresight or stacked-time solution methods)
% and explain how anticipated and unanticipated shocks need to be treated
% in such simulations. We run a Kalman filter that uses a nonlinear
% prediction step based on the same simulate technique.

%% How to Best Run This Tutorial?
%
% Each m-file in this tutorial is split into what is called "code sections"
% in Matlab. A code cell is a shorter block of code performing a specific
% task, separated from other code cells by a double percent sign, `%%`
% (usually with a title and brief introduction added). By default, the
% cells are visually separated from each other by a horizontal rule in the
% Matlab editor.
%
% Instead of running each m-file from the command window, or executing this
% `read_me_first` as a whole, do the following. Open one tutorial m-file in
% the Matlab editor. Arrange the editor window and the command window next
% to each other so that you can see both of them at the same time. Then run
% the m-file cell by cell. This will help you watch closely what exactly
% is going on.
%
% To execute one particular cell, place the cursor in that cell (the
% respective block of code will get highlighted), and select "Run Current
% Section" from a contextual menu (upon a right click on the mouse), or
% pressing a keyboard shortcut (which differ on different systems and
% Matlab versions). To learn more on code sections, search Matlab
% documentation for "code section".

%% Simple Endogenous Credibility Model
%
% This is a simple monetary policy model with two nonlinearities: a convex
% Phillips curve, and endogenous credibility of the central bank. Because
% we wish to preserve the nonlinearities in our simulations (using a
% simple perfect-foresight technique), we mark these equations with a `=#`
% symbol (in place of the usual equal sign). All other equations except
% these two will be simulated linearised.

edit credibility.model;

%% Read and Solve the Nonlinear Credibility Model
%
% This m-file is fairly standard. We read the endogenous credibility model
% file, `credibility.model`, assign some parameters, compute steady state,
% solve the model, and save everything for future use. These steps are
% explained in other tutorials in more detail.

% edit read_model.m;
read_model;

%% Simulate Nonlinearities During Disinflation
%
% Simulate the nonlinear credibility model to show different outcomes
% during disinflation, depending on the initial level of credibility.
% Evaluate the effect of nonlinearities compared to a linearized model.

% edit simulate_disinflation.m;
simulate_disinflation;

%% Stochastic Simulations in Nonlinear Models
%
% Set up and run simulations of unanticipated shocks. Non-linear
% simulations in IRIS are equivalent to a perfect-foresight solution. This
% does not mean unanticipated stochastic shocks cannot be simulated: it
% only means that simulations of such shocks must be split into a number of
% overlapping sub-simulations (called segments in IRIS), depending on the
% occurence of unanticipated shocks.

% edit stochastic_simulations.m;
stochastic_simulations;

%% Kalman Filter with Nonlinear Prediction Step
%
% Run the Kalman filter with a nonlinear prediction step
% to filter the data simulated previously in `stochastic_simulations`.
% With a linear prediction step, the credibility process doesn't respond to
% inflation performance and doesn't affect the rest of the economy. When
% the nonlinearity is preserved in the prediction step, the results get
% much more accurate.

% edit nonlinear_kalman.m;
nonlinear_kalman;

%% Publish Tutorial Files to PDFs
%
% The following commands can be used to create PDF versions of the tutorial
% files:

%{
    latex.publish('read_me_first.m',[],'evalCode=',false);
    latex.publish('credibility.model');
    latex.publish('read_model.m');
    latex.publish('simulate_disinflation.m');
    latex.publish('stochastic_simulations.m');
    latex.publish('nonlinear_kalman.m');
%}
